package com.scap.authen.dao;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Repository;

import com.scap.authen.entity.Site;

@Repository("voSiteDao")
public class SiteDaoImpl  extends AbstractDao  implements SiteDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getAllSite() {
		Criteria criteria = getSession().createCriteria(Site.class);	
		criteria.addOrder(Order.asc("vaSiteId"));
		return (List<Object>)criteria.list();
	}

	@Override
	public boolean insertSite(Site poSite) {
		boolean result = false;		
		try {
			persist(poSite);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}		
		return result;
	}

	@Override
	public boolean updateSite(Site poSite) {
		boolean result = false;
		
		try {
			
			Query query = getSession().createQuery("Update Site set vaSiteName = :vaSiteName ,"
					+ " vaStatus = :vaStatus "
    				+ " Where vaSiteId = :vaSiteId");
			
			query.setString("vaSiteName", poSite.getVaSiteName());
			query.setString("vaStatus", poSite.getVaStatus());
			query.setString("vaSiteId", poSite.getVaSiteId());
			query.executeUpdate();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getAllSiteAndRole() {
		Query query = getSession().createQuery("select site.vaSiteId , site.vaSiteName , role.vaRoleId , role.vaRoleName "
				+ "from Site as site , Role as role "
				+ "where site.vaSiteId=role.vaSiteId");	
		return (List<Object[]>)query.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getSiteList() {
		Query query = getSession().createQuery("select site.vaSiteId , site.vaSiteName"
				+ " from Site as site ");	
		return (List<Object[]>)query.list();
	}	
}
